var CryptoJS = require("./crypto-js")
let tools = {
  mappingObj: {},
  decryptEncryptAll: function (res, prop) {
    var json = JSON.parse(JSON.stringify(res)),
      base64Key = json.responseSecretKey,
      base64Iv = json.responseKeyOffset;

    return this.decryptContent(json[prop], base64Key, base64Iv);
  },
  decryptEncryptFields: function (res, prop, isJsonFormat) {
    var json = JSON.parse(JSON.stringify(res)),
      copyTxt = isJsonFormat ? json[prop] : JSON.parse(json[prop]),
      base64Key = json.responseSecretKey,
      base64Iv = json.responseKeyOffset,
      base64Mapping = json.mapping,
      encryptField = json.encryptFields,
      mapping = this.decryptContent(base64Mapping, base64Key, base64Iv),
      mappings = mapping.split(",");

    for (var i = 0; i < mappings.length; i++) {
      var relative = mappings[i].split("-");
      this.mappingObj[relative[1]] = String(relative[0]);
    }
    return this.modifyJson(copyTxt, encryptField);
  },
  decryptContent: function (content, key, iv) {
    var keyStr = CryptoJS.enc.Base64.parse(key),
      ivStr = CryptoJS.enc.Base64.parse(iv),
      base64 = CryptoJS.enc.Base64.parse(content),
      srcs = CryptoJS.enc.Base64.stringify(base64),
      descrypted = CryptoJS.AES.decrypt(srcs, keyStr, {
        iv: ivStr,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7,
      });
    return CryptoJS.enc.Utf8.stringify(descrypted);
  },
  replaceStr: function (str, index, char) {
    var strAry = str.split("");
    strAry[index] = char;
    return strAry.join("");
  },
  // 定义一个函数来修改JSON对象的每个字段值
  modifyJson: function (obj, encryptField) {
    for (var key in obj) {
      // 如果当前属性值是字符串，将其转换为大写
      if (typeof obj[key] === "string") {
        for (var i = 0; i < obj[key].length; i++) {
          if (encryptField.indexOf(key) != -1) {
            var tag = obj[key].charAt(i);
            if (this.mappingObj[tag]) {
              obj[key] = this.replaceStr(obj[key], i, this.mappingObj[tag]);
            }
          }
        }
      } else if (Array.isArray(obj[key])) {
        // 如果当前属性值是数组，将数组中的每个元素乘以2
        for (var i = 0; i < obj[key].length; i++) {
          if (typeof obj[key] === "object" && obj[key] !== null) {
            this.modifyJson(obj[key], encryptField);
          }
          if (encryptField.indexOf(key) != -1) {
            for (var j = 0; j < obj[key][i].length; j++) {
              var tag = obj[key][i].charAt(j);
              if (this.mappingObj[tag]) {
                obj[key][i] = this.replaceStr(
                  obj[key][i],
                  j,
                  this.mappingObj[tag]
                );
              }
            }
          }
        }
      } else if (typeof obj[key] === "object" && obj[key] !== null) {
        // 如果当前属性值是对象，递归调用modifyJson函数
        this.modifyJson(obj[key], encryptField);
      }
    }
    return obj;
  },
};

function decryptEncryptFields(res, field, isJson){
  return tools.decryptEncryptFields(res, field, isJson);
}


function say_hello(name){
  return "hello" + name
}